package com.yanqu.road.server.cmd.activity.dafuweng;

import com.yanqu.road.entity.activity.dafuweng.DfwAttackBossResult;
import com.yanqu.road.server.cmd.CrossUnionCommand;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.monopoly.MonopolyProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.cross.battle.DaFuWengMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.util.ArrayList;
import java.util.List;

@Cmd(code = CrossProtocol.C_CROSS_DA_FU_WENG_TEN_ROLL_CROSS, desc = "")
public class CrossDaFuWengTenRollDealCmd extends CrossUnionCommand {
    List<MonopolyProto.MonopolyUnionMsg> unionMsgList = new ArrayList<>();

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        MonopolyProto.DfwTenRollCrossReqMsg req = MonopolyProto.DfwTenRollCrossReqMsg.parseFrom(packet.getBytes());
        Property reward = new Property();
        String reqReward = req.getReward();
        if (reqReward != null) {
            Property property = PropertyHelper.parseStringToProperty(reqReward);
            reward.addProperty(property);
        }
        int activityId = req.getActivityId();
        long userId = req.getUserId();
        MonopolyProto.MonopolyPlayerMsg monopolyPlayer = req.getMonopolyPlayer();
        List<MonopolyProto.DfwTenRollAttackBossMsg> bossDamageMsgList = req.getBossDamageMsgList();
        boolean isZaoYao = req.getZaoyao();
        String unionUid = monopolyPlayer.getPlayer().getUnionUid();
        if (isZaoYao) {
            unionMsgList = DaFuWengMgr.getRandomZaoYaoUnionMsg(activityId, unionUid);
        }
        int totalDamage = 0;
        Property needAddReward = new Property();
        MonopolyProto.DfwTenRollCrossRespMsg.Builder resp = MonopolyProto.DfwTenRollCrossRespMsg.newBuilder();
        for (MonopolyProto.DfwTenRollAttackBossMsg bossMsg : bossDamageMsgList) {
            DfwAttackBossResult result = DaFuWengMgr.tenRollShareAndAttackBoss(activityId, serverId, userId, bossMsg.getDamage(), monopolyPlayer);
            if (result != null) {
                Property property = result.getReward();
                reward.addProperty(property);
                needAddReward.addProperty(property);
                int realDamage = result.getRealDamage();
                totalDamage += realDamage;
                MonopolyProto.DfwAttackBossLogMsg.Builder bossLog = MonopolyProto.DfwAttackBossLogMsg.newBuilder();
                bossLog.setBossId(result.getBossId());
                bossLog.setBossType(result.getBossType());
                bossLog.setDamage(result.getRealDamage());
                bossLog.setKill(result.isKill());
                bossLog.setReward(PropertyHelper.parsePropertyToString(result.getReward()));
                resp.addDfwBossLog(bossLog);
            }
        }
        resp.setActivityId(activityId);
        resp.setRewardData(PropertyHelper.parsePropertyToString(reward));
        resp.addAllUnionMsgList(unionMsgList);
        if (needAddReward.isNothing()) {
            resp.setNeedAddReward("");
        }else {
            resp.setNeedAddReward(PropertyHelper.parsePropertyToString(needAddReward));
        }
        resp.setAddPower(req.getAddPower());
        resp.setRealDamage(totalDamage);
        resp.setZaoyao(req.getZaoyao());
        resp.setRollTimes(req.getRollTimes());
        YanQuMessage message = YanQuMessageUtils.buildMessage(GameProtocol.S_DA_FU_WENG_CROSS_TEN_ROLL_RESULT, resp);
        MessageHelper.sendPacket(serverId, userId, message);
    }

    @Override
    public void sendData() {

    }
}
